# Topologia
# 
#  pkt=1000B, 100pkts/s  =>  bw= 0,8M
#  s0-------N0
#            \  1M
#             \    Q=Stochastic Fair Queuing
#              N2 ----------------------- N3
#             /            1M
#            /  1M
#  s1-------N1
#  pkt=500B, 200pkts/s => bw = 0,8M
#
#
#            +cbr1                         -cbr1
#       +cbr0                                   -cbr0
#  +--------+---------+----------+---------+---------+
#  0        1         2          3         4         5

set ns [new Simulator]

$ns color 1 Blue
$ns color 2 Red

set nf [open out.nam w]
set f [open out.tr w]

$ns trace-all $f
$ns namtrace-all $nf

proc finish {} {
	global ns nf
	global ns f
	
	$ns flush-trace
	close $nf
	close $f
	
	#Executar simulador
	exec nam out.nam &
	exit 0
}

#Criar nós
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Criar links
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms SFQ

#???: Organizacao no simulador grafico ???
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

$ns duplex-link-op $n2 $n3 queuePos 0.5

#Agentes
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
$udp0 set fid_ 1

set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set packetSize_ 1000
$cbr0 set interval_ 0.01

set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
$udp0 set fid_ 2

set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005

set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

#Conexao com o trafego
$ns connect $udp0 $null0
$ns connect $udp1 $null0

#Inicio e fim dos agente CBR
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"


$ns run